import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
import 'package:baseProjectFlutter/base/mvvm/base_mvvm_state.dart';
import 'package:baseProjectFlutter/base/mvvm/base_state.dart';
import 'package:baseProjectFlutter/page/vm/login_vm.dart';
import 'package:baseProjectFlutter/router/app_routes_manager.dart';
import 'package:baseProjectFlutter/utils/live_data/event_bus.dart';
import 'package:baseProjectFlutter/widgets_common/image/svg_widget.dart';

import '../../config/app_colors.dart';
import '../../generated/assets.dart';
import '../../utils/other/log/log.dart';

class LoginPage extends BasePage {
  const LoginPage({super.key});

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends BaseMvvmState<LoginPage, LoginVm> {
  _LoginPageState() : super(LoginVm());

  @override
  void initState() {
    super.initState();
    LogUtils.e("登录页面: ${vm.hashCode}");
    EventBusKey.TEST.bus(this, (data) {
      LogUtils.e("收到事件: $data");
      AppRouterJump.back();
    });
    title = "登录";
  }

  @override
  PreferredSizeWidget? createAppbar() => AppBar(
        title: Obx(() => Text(
              "$title,${vm.counter}",
              style: const TextStyle(fontSize: 16, color: AppColors.black_33),
            )),
        centerTitle: true,
        backgroundColor: AppColors.white,
      );

  @override
  Widget? floatingActionButton() {
    return FloatingActionButton(
      heroTag: hashCode,
      onPressed: () {
        vm.incrementCounter();
      },
      tooltip: 'Increment',
      child: const Icon(Icons.add),
    );
  }

  @override
  Widget buildWidget(BuildContext context) {
    return SingleChildScrollView(
      child: SizedBox(
        width: double.infinity,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            SizedBox(
              height: 40,
              child: ElevatedButton(
                  onPressed: () {
                    EventBusKey.TEST.busPost("测试数据");
                  },
                  child: const Text("发送事件")),
            ),
            Container(
              margin: const EdgeInsets.only(top: 10),
              constraints: const BoxConstraints(
                maxWidth: 200,
                maxHeight: 40,
              ),
              child: TextField(
                decoration: const InputDecoration(
                  contentPadding: EdgeInsets.all(10),
                  hintText: "输入1",
                  border: OutlineInputBorder(),
                  floatingLabelBehavior: FloatingLabelBehavior.never,
                ),
                keyboardType: TextInputType.number,
                inputFormatters: [
                  // FilteringTextInputFormatter.digitsOnly,
                  // 只允许输入数字
                  FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d*$')),
                  // 只允许输入数字和小数点
                ],
                onChanged: (value) {
                  LogUtils.e("输入1改变: $value");
                },
              ),
            ),
            Image.asset(
              Assets.imgIcTips,
              width: 100,
              height: 100,
            ),
            SvgWidget(Assets.svgIcSuccess, width: 100, height: 100),
            Text(
              '${vm.counter}',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
    );
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // TODO: implement didChangeAppLifecycleState
    super.didChangeAppLifecycleState(state);
    LogUtils.e("didChangeAppLifecycleState: $state");
  }

  @override
  void dispose() {
    super.dispose();
  }
}
